package com.ruoyi.purchase.service.impl;

import com.ruoyi.purchase.domain.AllocationPlan;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.domain.SupplyDetail;
import com.ruoyi.purchase.mapper.AllocationPlanMapper;
import com.ruoyi.purchase.mapper.OrderDetailMapper;
import com.ruoyi.purchase.mapper.SupplyDetailMapper;
import com.ruoyi.purchase.service.ISupplyDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * supplyDetailService业务层处理
 * 
 * @author kevinness
 * @date 2025-01-15
 */
@Service
public class SupplyDetailService implements ISupplyDetailService
{
    @Autowired
    private SupplyDetailMapper supplyDetailMapper;

    @Autowired
    private AllocationPlanMapper allocationPlanMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询supplyDetail
     *
     * @param rawProductBatch supplyDetail主键
     * @return supplyDetail
     */
    @Override
    public SupplyDetail selectSupplyDetailByRawProductBatch(String rawProductBatch)
    {
        return supplyDetailMapper.selectSupplyDetailByRawProductBatch(rawProductBatch);
    }

    /**
     * 查询supplyDetail列表
     *
     * @param supplyDetail supplyDetail
     * @return supplyDetail
     */
    @Override
    public List<SupplyDetail> selectSupplyDetailList(SupplyDetail supplyDetail)
    {
        return supplyDetailMapper.selectSupplyDetailList(supplyDetail);
    }

    /**
     * 新增supplyDetail
     *
     * @param supplyDetail supplyDetail
     * @return 结果
     */
    @Override
    public int insertSupplyDetail(SupplyDetail supplyDetail)
    {
        return supplyDetailMapper.insertSupplyDetail(supplyDetail);
    }

    /**
     * 批量新增货源详情
     *
     * @param supplyDetailList 货源详情列表
     * @return 结果
     */
    @Override
    public int insertSupplyDetailList(List<SupplyDetail> supplyDetailList){
        int result = 1;
        for (SupplyDetail supplyDetail : supplyDetailList) {
            result = insertSupplyDetail(supplyDetail);
            if (result == 0) {
                return result;
            }
        }
        return result;
    }

    /**
     * 修改supplyDetail
     *
     * @param supplyDetail supplyDetail
     * @return 结果
     */
    @Override
    public int updateSupplyDetail(SupplyDetail supplyDetail)
    {
        return supplyDetailMapper.updateSupplyDetail(supplyDetail);
    }

    /**
     * 批量删除supplyDetail
     *
     * @param rawProductBatchs 需要删除的supplyDetail主键
     * @return 结果
     */
    @Override
    public int deleteSupplyDetailByRawProductBatchs(String[] rawProductBatchs)
    {
        return supplyDetailMapper.deleteSupplyDetailByRawProductBatchs(rawProductBatchs);
    }

    /**
     * 删除supplyDetail信息
     *
     * @param rawProductBatch supplyDetail主键
     * @return 结果
     */
    @Override
    public int deleteSupplyDetailByRawProductBatch(String rawProductBatch)
    {
        return supplyDetailMapper.deleteSupplyDetailByRawProductBatch(rawProductBatch);
    }

    /**
     * 批量更新货源详情
     *
     * @param supplyDetailList 货源详情
     * @return 结果
     */
    @Override
    public int updateSupplyDetailList(List<SupplyDetail> supplyDetailList){
        int result = 1;
        for (SupplyDetail supplyDetail : supplyDetailList) {
            SupplyDetail detail = supplyDetailMapper.selectSupplyDetailByRawProductBatch(supplyDetail.getRawProductBatch());
            if (detail != null) {
                result = supplyDetailMapper.updateSupplyDetail(supplyDetail);
            }else{
                result = supplyDetailMapper.insertSupplyDetail(supplyDetail);
            }

            if (result == 0) {
                return result;
            }
        }
        return result;
    }

    /**
     * 根据调拨编号获取货源详情
     *
     * @param allocationNo 调拨编号
     * @return 结果
     */
    @Override
    public List<SupplyDetail> getSupplyDetailListByAllocationNo(String allocationNo){
        AllocationPlan allocationPlan = allocationPlanMapper
                .selectAllocationPlanByAllocationNo(allocationNo);
        List<OrderDetail> orderDetails = orderDetailMapper
                .selectOrderDetailByOrderNo(allocationPlan.getOrderNo());
        List<SupplyDetail> supplyDetails = new ArrayList<SupplyDetail>();
        for (OrderDetail orderDetail : orderDetails) {
            supplyDetails.add(selectSupplyDetailByRawProductBatch(orderDetail.getRawProductBatch()));
        }
        return supplyDetails;
    }
}